package net.lanelife.casino.caipiao.entity;

import java.text.ParseException;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;

import net.lanelife.casino.common.entity.annotation.Checked;
import net.lanelife.casino.common.entity.enumeration.DataType;
import net.lanelife.casino.common.entity.support.CheckableEntity;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@Entity
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "TB_PREFERENCE")
public class Preference extends CheckableEntity<Integer> {

	private static final long serialVersionUID = -8061702407266997556L;

	@Checked
	@Column(name = "NAME", length = 20, nullable = false)
	private String name;

	@Checked
	@Column(name = "CODE", length = 200, nullable = false, unique = true)
	private String code;

	@Checked
	@Column(name = "VALUE", length = 200, nullable = false)
	private String value;

	@Column(name = "DESCRIPTION", length = 200, nullable = false)
	private String description;

	@Checked
	@Enumerated(EnumType.ORDINAL)
	@Column(name = "TYPE", nullable = false)
	private Type type;

	@Checked
	@Enumerated(EnumType.ORDINAL)
	@Column(name = "DATA_TYPE", nullable = false)
	private DataType dataType;

	@Checked
	@Column(name = "PARENT_ID")
	private Integer parentId;

	public static enum Type {
		MENU, CONFIG
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public DataType getDataType() {
		return dataType;
	}

	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}
	
	public Object parseValue() throws ParseException {
		Object val = null;
		switch (this.dataType) {
		case STRING:
			val = value;
			break;
		case INTEGER:
			val = Integer.valueOf(value);
			break;
		case NUMERIC:
			val = Double.valueOf(value);
			break;
		case BOOLEAN:
			val = Boolean.valueOf(value);
			break;
		case DATETIME:
			val = DateUtils.parseDate(value, new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"});
			break;
		default:
			val = value;
			break;
		}
		return val;
	}
	
	public String stringValue() throws ParseException {
		return (String) parseValue();
	}
	
	public Integer intValue() throws ParseException {
		return (Integer) parseValue();
	}
	
	public Double doubleValue() throws ParseException {
		return (Double) parseValue();
	}
	
	public Boolean booleanValue() throws ParseException {
		return (Boolean) parseValue();
	}
	
	public Date datetimeValue() throws ParseException {
		return (Date) parseValue();
	}

}
